class demo { public static void main(String[] args) { ArrayList arrayList=new ArrayList(); arrayList.add(2); arrayList.add(4); arrayList.add(6); arrayList.add(10); arrayList.add(12); System.out.print(arrayList); } }
ArrayList is [2,4,6,10,12]
class demo { public static void main(String[] args) { ArrayList arrayList=new ArrayList(); arrayList.add(1001); arrayList.add("Amit Jain"); arrayList.add("21"); System.out.print(arrayList); } }
ArrayList is [1001,Amit Jain,21]
import java.util.*; class demo { public static void main(String[] args) { ArrayList<Integer> arrayList=new ArrayList<>(); Scanner scan=new Scanner(System.in); System.out.println("Enter How Many Numbers"); int n=scan.nextInt(); System.out.printf("Enter %d Numbers",n); for(int i=1;i<=n;i++) arrayList.add(scan.nextInt()); System.out.println(arrayList); } }
Enter how many numbers 6 Enter 6 numbers 2 3 4 1 6 8 ArrayList is [2, 3, 4, 1, 6, 8]
class demo { public static void main(String[] args) { ArrayList arrayList=new ArrayList(); Scanner scan=new Scanner(System.in); System.out.println("Enter How Many Numbers"); int n=scan.nextInt(); System.out.printf("Enter %d Numbers",n); for(int i=1;i<=n;i++) arrayList.add(scan.nextInt()); int s=0; for(int i=0;i<arrayList.size();i++) s=s+(int)arrayList.get(i); System.out.println("Sum is "+s); } }
Enter how many numbers 6 Enter 6 numbers 2 3 4 1 6 8 Sum is 24
import java.util.*; class demo { public static void main(String[] args) { ArrayList<Integer> arrayList=new ArrayList<>(); Scanner scan=new Scanner(System.in); System.out.println("Enter How Many Numbers"); int n=scan.nextInt(); System.out.printf("Enter %d Numbers",n); for(int i=1;i<=n;i++) arrayList.add(scan.nextInt()); int s=0; for(int item:arrayList) if(item%2==0) s=s+item; System.out.println("Sum of even is "+s); } }
Enter how many numbers 6 Enter 6 numbers 2 3 4 1 6 8 Sum of even is 20
import java.util.*; class demo { public static void main(String[] args) { ArrayList<Integer> arrayList=new ArrayList<>(); Scanner scan=new Scanner(System.in); System.out.println("Enter How Many Numbers"); int n=scan.nextInt(); System.out.printf("Enter %d Numbers",n); for(int i=1;i<=n;i++) arrayList.add(scan.nextInt()); int c=0; for(int item:arrayList) if(item>0) c=c+1; System.out.println("Total Negative no. are "+c); } }
Enter how many numbers 8 Enter 8 numbers 2 -3 4 -1 6 -8 7 5 Total positive no. are 5
class demo { public static void main(String[] args) { Vector vector=new Vector(); Scanner scan=new Scanner(System.in); System.out.println("Enter How Many Numbers"); int n=scan.nextInt(); System.out.printf("Enter %d Numbers",n); for(int i=1;i<=n;i++) vector.add(scan.nextInt()); int c=0; for(int i=0;i<vector.size();i++) { int val=(int)vector.get(i); if(val%2==0) c=c+1; } System.out.println("Total even no. are "+c); } }
Enter how many numbers 8 Enter 8 numbers 2 3 4 1 6 8 10 9 Total even no. are 5